<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>textrangeiterator.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="local_closure_goog_dom_textrangeiterator.js.html">textrangeiterator.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2007 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Iterator between two DOM text range positions.
<a name="line17"></a> *
<a name="line18"></a> * @author robbyw@google.com (Robby Walker)
<a name="line19"></a> */
<a name="line20"></a>
<a name="line21"></a>goog.provide(&#39;goog.dom.TextRangeIterator&#39;);
<a name="line22"></a>
<a name="line23"></a>goog.require(&#39;goog.array&#39;);
<a name="line24"></a>goog.require(&#39;goog.dom.NodeType&#39;);
<a name="line25"></a>goog.require(&#39;goog.dom.RangeIterator&#39;);
<a name="line26"></a>goog.require(&#39;goog.dom.TagName&#39;);
<a name="line27"></a>goog.require(&#39;goog.iter.StopIteration&#39;);
<a name="line28"></a>
<a name="line29"></a>
<a name="line30"></a>
<a name="line31"></a>/**
<a name="line32"></a> * Subclass of goog.dom.TagIterator that iterates over a DOM range.  It
<a name="line33"></a> * adds functions to determine the portion of each text node that is selected.
<a name="line34"></a> *
<a name="line35"></a> * @param {Node} startNode The starting node position.
<a name="line36"></a> * @param {number} startOffset The offset in to startNode.  If startNode is
<a name="line37"></a> *     an element, indicates an offset in to childNodes.  If startNode is a
<a name="line38"></a> *     text node, indicates an offset in to nodeValue.
<a name="line39"></a> * @param {Node} endNode The ending node position.
<a name="line40"></a> * @param {number} endOffset The offset in to endNode.  If endNode is
<a name="line41"></a> *     an element, indicates an offset in to childNodes.  If endNode is a
<a name="line42"></a> *     text node, indicates an offset in to nodeValue.
<a name="line43"></a> * @param {boolean=} opt_reverse Whether to traverse nodes in reverse.
<a name="line44"></a> * @constructor
<a name="line45"></a> * @extends {goog.dom.RangeIterator}
<a name="line46"></a> * @final
<a name="line47"></a> */
<a name="line48"></a>goog.dom.TextRangeIterator = function(startNode, startOffset, endNode,
<a name="line49"></a>    endOffset, opt_reverse) {
<a name="line50"></a>  var goNext;
<a name="line51"></a>
<a name="line52"></a>  if (startNode) {
<a name="line53"></a>    this.startNode_ = startNode;
<a name="line54"></a>    this.startOffset_ = startOffset;
<a name="line55"></a>    this.endNode_ = endNode;
<a name="line56"></a>    this.endOffset_ = endOffset;
<a name="line57"></a>
<a name="line58"></a>    // Skip to the offset nodes - being careful to special case BRs since these
<a name="line59"></a>    // have no children but still can appear as the startContainer of a range.
<a name="line60"></a>    if (startNode.nodeType == goog.dom.NodeType.ELEMENT &amp;&amp;
<a name="line61"></a>        startNode.tagName != goog.dom.TagName.BR) {
<a name="line62"></a>      var startChildren = startNode.childNodes;
<a name="line63"></a>      var candidate = startChildren[startOffset];
<a name="line64"></a>      if (candidate) {
<a name="line65"></a>        this.startNode_ = candidate;
<a name="line66"></a>        this.startOffset_ = 0;
<a name="line67"></a>      } else {
<a name="line68"></a>        if (startChildren.length) {
<a name="line69"></a>          this.startNode_ =
<a name="line70"></a>              /** @type {Node} */ (goog.array.peek(startChildren));
<a name="line71"></a>        }
<a name="line72"></a>        goNext = true;
<a name="line73"></a>      }
<a name="line74"></a>    }
<a name="line75"></a>
<a name="line76"></a>    if (endNode.nodeType == goog.dom.NodeType.ELEMENT) {
<a name="line77"></a>      this.endNode_ = endNode.childNodes[endOffset];
<a name="line78"></a>      if (this.endNode_) {
<a name="line79"></a>        this.endOffset_ = 0;
<a name="line80"></a>      } else {
<a name="line81"></a>        // The offset was past the last element.
<a name="line82"></a>        this.endNode_ = endNode;
<a name="line83"></a>      }
<a name="line84"></a>    }
<a name="line85"></a>  }
<a name="line86"></a>
<a name="line87"></a>  goog.dom.RangeIterator.call(this, opt_reverse ? this.endNode_ :
<a name="line88"></a>      this.startNode_, opt_reverse);
<a name="line89"></a>
<a name="line90"></a>  if (goNext) {
<a name="line91"></a>    try {
<a name="line92"></a>      this.next();
<a name="line93"></a>    } catch (e) {
<a name="line94"></a>      if (e != goog.iter.StopIteration) {
<a name="line95"></a>        throw e;
<a name="line96"></a>      }
<a name="line97"></a>    }
<a name="line98"></a>  }
<a name="line99"></a>};
<a name="line100"></a>goog.inherits(goog.dom.TextRangeIterator, goog.dom.RangeIterator);
<a name="line101"></a>
<a name="line102"></a>
<a name="line103"></a>/**
<a name="line104"></a> * The first node in the selection.
<a name="line105"></a> * @type {Node}
<a name="line106"></a> * @private
<a name="line107"></a> */
<a name="line108"></a>goog.dom.TextRangeIterator.prototype.startNode_ = null;
<a name="line109"></a>
<a name="line110"></a>
<a name="line111"></a>/**
<a name="line112"></a> * The last node in the selection.
<a name="line113"></a> * @type {Node}
<a name="line114"></a> * @private
<a name="line115"></a> */
<a name="line116"></a>goog.dom.TextRangeIterator.prototype.endNode_ = null;
<a name="line117"></a>
<a name="line118"></a>
<a name="line119"></a>/**
<a name="line120"></a> * The offset within the first node in the selection.
<a name="line121"></a> * @type {number}
<a name="line122"></a> * @private
<a name="line123"></a> */
<a name="line124"></a>goog.dom.TextRangeIterator.prototype.startOffset_ = 0;
<a name="line125"></a>
<a name="line126"></a>
<a name="line127"></a>/**
<a name="line128"></a> * The offset within the last node in the selection.
<a name="line129"></a> * @type {number}
<a name="line130"></a> * @private
<a name="line131"></a> */
<a name="line132"></a>goog.dom.TextRangeIterator.prototype.endOffset_ = 0;
<a name="line133"></a>
<a name="line134"></a>
<a name="line135"></a>/** @override */
<a name="line136"></a>goog.dom.TextRangeIterator.prototype.getStartTextOffset = function() {
<a name="line137"></a>  // Offsets only apply to text nodes.  If our current node is the start node,
<a name="line138"></a>  // return the saved offset.  Otherwise, return 0.
<a name="line139"></a>  return this.node.nodeType != goog.dom.NodeType.TEXT ? -1 :
<a name="line140"></a>         this.node == this.startNode_ ? this.startOffset_ : 0;
<a name="line141"></a>};
<a name="line142"></a>
<a name="line143"></a>
<a name="line144"></a>/** @override */
<a name="line145"></a>goog.dom.TextRangeIterator.prototype.getEndTextOffset = function() {
<a name="line146"></a>  // Offsets only apply to text nodes.  If our current node is the end node,
<a name="line147"></a>  // return the saved offset.  Otherwise, return the length of the node.
<a name="line148"></a>  return this.node.nodeType != goog.dom.NodeType.TEXT ? -1 :
<a name="line149"></a>      this.node == this.endNode_ ? this.endOffset_ : this.node.nodeValue.length;
<a name="line150"></a>};
<a name="line151"></a>
<a name="line152"></a>
<a name="line153"></a>/** @override */
<a name="line154"></a>goog.dom.TextRangeIterator.prototype.getStartNode = function() {
<a name="line155"></a>  return this.startNode_;
<a name="line156"></a>};
<a name="line157"></a>
<a name="line158"></a>
<a name="line159"></a>/**
<a name="line160"></a> * Change the start node of the iterator.
<a name="line161"></a> * @param {Node} node The new start node.
<a name="line162"></a> */
<a name="line163"></a>goog.dom.TextRangeIterator.prototype.setStartNode = function(node) {
<a name="line164"></a>  if (!this.isStarted()) {
<a name="line165"></a>    this.setPosition(node);
<a name="line166"></a>  }
<a name="line167"></a>
<a name="line168"></a>  this.startNode_ = node;
<a name="line169"></a>  this.startOffset_ = 0;
<a name="line170"></a>};
<a name="line171"></a>
<a name="line172"></a>
<a name="line173"></a>/** @override */
<a name="line174"></a>goog.dom.TextRangeIterator.prototype.getEndNode = function() {
<a name="line175"></a>  return this.endNode_;
<a name="line176"></a>};
<a name="line177"></a>
<a name="line178"></a>
<a name="line179"></a>/**
<a name="line180"></a> * Change the end node of the iterator.
<a name="line181"></a> * @param {Node} node The new end node.
<a name="line182"></a> */
<a name="line183"></a>goog.dom.TextRangeIterator.prototype.setEndNode = function(node) {
<a name="line184"></a>  this.endNode_ = node;
<a name="line185"></a>  this.endOffset_ = 0;
<a name="line186"></a>};
<a name="line187"></a>
<a name="line188"></a>
<a name="line189"></a>/** @override */
<a name="line190"></a>goog.dom.TextRangeIterator.prototype.isLast = function() {
<a name="line191"></a>  return this.isStarted() &amp;&amp; this.node == this.endNode_ &amp;&amp;
<a name="line192"></a>      (!this.endOffset_ || !this.isStartTag());
<a name="line193"></a>};
<a name="line194"></a>
<a name="line195"></a>
<a name="line196"></a>/**
<a name="line197"></a> * Move to the next position in the selection.
<a name="line198"></a> * Throws {@code goog.iter.StopIteration} when it passes the end of the range.
<a name="line199"></a> * @return {Node} The node at the next position.
<a name="line200"></a> * @override
<a name="line201"></a> */
<a name="line202"></a>goog.dom.TextRangeIterator.prototype.next = function() {
<a name="line203"></a>  if (this.isLast()) {
<a name="line204"></a>    throw goog.iter.StopIteration;
<a name="line205"></a>  }
<a name="line206"></a>
<a name="line207"></a>  // Call the super function.
<a name="line208"></a>  return goog.dom.TextRangeIterator.superClass_.next.call(this);
<a name="line209"></a>};
<a name="line210"></a>
<a name="line211"></a>
<a name="line212"></a>/** @override */
<a name="line213"></a>goog.dom.TextRangeIterator.prototype.skipTag = function() {
<a name="line214"></a>  goog.dom.TextRangeIterator.superClass_.skipTag.apply(this);
<a name="line215"></a>
<a name="line216"></a>  // If the node we are skipping contains the end node, we just skipped past
<a name="line217"></a>  // the end, so we stop the iteration.
<a name="line218"></a>  if (goog.dom.contains(this.node, this.endNode_)) {
<a name="line219"></a>    throw goog.iter.StopIteration;
<a name="line220"></a>  }
<a name="line221"></a>};
<a name="line222"></a>
<a name="line223"></a>
<a name="line224"></a>/** @override */
<a name="line225"></a>goog.dom.TextRangeIterator.prototype.copyFrom = function(other) {
<a name="line226"></a>  this.startNode_ = other.startNode_;
<a name="line227"></a>  this.endNode_ = other.endNode_;
<a name="line228"></a>  this.startOffset_ = other.startOffset_;
<a name="line229"></a>  this.endOffset_ = other.endOffset_;
<a name="line230"></a>  this.isReversed_ = other.isReversed_;
<a name="line231"></a>
<a name="line232"></a>  goog.dom.TextRangeIterator.superClass_.copyFrom.call(this, other);
<a name="line233"></a>};
<a name="line234"></a>
<a name="line235"></a>
<a name="line236"></a>/**
<a name="line237"></a> * @return {!goog.dom.TextRangeIterator} An identical iterator.
<a name="line238"></a> * @override
<a name="line239"></a> */
<a name="line240"></a>goog.dom.TextRangeIterator.prototype.clone = function() {
<a name="line241"></a>  var copy = new goog.dom.TextRangeIterator(this.startNode_,
<a name="line242"></a>      this.startOffset_, this.endNode_, this.endOffset_, this.isReversed_);
<a name="line243"></a>  copy.copyFrom(this);
<a name="line244"></a>  return copy;
<a name="line245"></a>};
</pre>


</body>
</html>
